package com.gewu.pm.ui.fragment.approval;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.bean.ApprovalApplicationBean;
import com.gewu.pm.other.IntentKey;
import com.gewu.pm.ui.activity.clockin.MyMakeUpApprovalDetailActivity;
import com.gewu.pm.ui.adapter.approval.CcToMeAdapter;
import com.hjq.base.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 抄送我的 Fragment
 * 通过 type 参数区分：0=待审批，1=已审批
 */
public final class CcToMePendingFragment extends AppFragment<AppActivity>
        implements BaseAdapter.OnItemClickListener {

    private RecyclerView mRecyclerView;
    private CcToMeAdapter mAdapter;
    private List<ApprovalApplicationBean> mDataList = new ArrayList<>();
    private int mType; // 0=待审批，1=已审批

    /**
     * 创建待审批 Fragment
     */
    public static CcToMePendingFragment newInstance() {
        return newInstance(0);
    }

    /**
     * 创建 Fragment
     *
     * @param type 0=待审批，1=已审批
     */
    public static CcToMePendingFragment newInstance(int type) {
        CcToMePendingFragment fragment = new CcToMePendingFragment();
        Bundle args = new Bundle();
        args.putInt(IntentKey.TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.public_only_recycler_view;
    }

    @Override
    protected void initView() {
        mRecyclerView = findViewById(R.id.rv_list);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new CcToMeAdapter(getContext());
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        // 获取传入的 type 参数
        Bundle args = getArguments();
        if (args != null) {
            mType = args.getInt(IntentKey.TYPE, 0);
        }

        loadData();
    }

    /**
     * 加载数据（创建测试数据）
     */
    private void loadData() {
        mDataList.clear();

        if (mType == 0) {
            // 待审批数据
            ApprovalApplicationBean bean1 = new ApprovalApplicationBean(
                    "1",
                    "补卡",
                    "该区域为补卡事由",
                    1  // 待审批
            );

            ApprovalApplicationBean bean2 = new ApprovalApplicationBean(
                    "2",
                    "补卡",
                    "该区域为补卡事由",
                    1  // 待审批
            );

            mDataList.add(bean1);
            mDataList.add(bean2);
        } else {
            // 已审批数据（包含已通过和已驳回）
            ApprovalApplicationBean bean1 = new ApprovalApplicationBean(
                    "1",
                    "补卡",
                    "该区域为补卡事由",
                    2  // 已通过
            );

            ApprovalApplicationBean bean2 = new ApprovalApplicationBean(
                    "2",
                    "补卡",
                    "该区域为补卡事由",
                    3  // 已驳回
            );

            mDataList.add(bean1);
            mDataList.add(bean2);
        }

        mAdapter.setData(mDataList);
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        ApprovalApplicationBean bean = mAdapter.getItem(position);
        // TODO: 跳转到详情页
        // 跳转到审批详情页面，传递id和type
        MyMakeUpApprovalDetailActivity.start(getAttachActivity(), bean.getId(), bean.getType() + "");
    }
}
